require File.dirname(__FILE__) + '/canonizer'

class Document
  # Represents the text contents of a file. 
  # This is used for creating suffix arrays (see the wiki)
  # and finding files that partially match. 
  
  attr_reader :rep

  def initialize(words)
    if words.respond_to? :split
      @rep = Canonizer.canonize(words).split
    else
      @rep = words.map {|word| Canonizer.canonize(word)}
    end
  end

  def create_suffixes
    if Suffix.match_length.nil?
      max_suffixes = @rep.length-1
    else
      max_suffixes = @rep.length - Suffix.match_length
    end
    (0..max_suffixes).map{ |i| Suffix.new(i, self) }
  end

  def words(offset=0)
    @rep[offset..-1]
  end

  def ==(other)
    @rep == other.rep
  end

  def [](index)
    @rep[index]
  end

  def length
    @rep.length
  end
end
